package com.xl.singletonPattern.practice;
/**
 * 缓存管理器测试类
 */
public class CacheManagerTest {
    public static void main(String[] args) {
        System.out.println("=== 缓存管理器单例测试 ===");
        // 测试单例特性
        //testSingleProperty();
        // 测试基本功能
        //testBasicFunction();
        // 测试过期功能
        //testExpiration();
        // 测试多线程环境
        //testMultiThread();
        // 测试类型安全获取
        testTypeSafeGet();
    }

    /**
     * 测试单例特性
     */
    private static void testSingleProperty() {
        System.out.println("1.测试单例特性");
        CacheManager cache1  = CacheManager.INSTANCE;
        CacheManager cache2  = CacheManager.INSTANCE;
        CacheManager cache3  = CacheManager.INSTANCE;
        System.out.println("cache1==cache2:"+(cache1==cache2));
        System.out.println("cache1==cache3:"+(cache1==cache3));
    }
    /**
     * 测试基本功能
     */
    private static void testBasicFunction() {
        System.out.println("2.测试基本功能");
        CacheManager cache = CacheManager.INSTANCE;
        cache.put("name", "张三");
        cache.put("age", 18);
        cache.put("address", "北京");
        cache.get("name");
        cache.get("age");
        cache.get("address");
        cache.get("xx");//测试不存在的key
        System.out.println("当前缓存大小："+cache.size());
    }
    /**
     * 测试缓存过期
     */
    private static void testExpiration() {
        System.out.println("3.测试缓存过期");
        CacheManager cache = CacheManager.INSTANCE;
        cache.put("name", "张三", 1000);
        cache.get("name");
        //休眠1秒
        try {
            Thread.sleep(1500);
            cache.get("name");
        } catch (InterruptedException e) {
            e.printStackTrace(System.err);
        }
    }
    /**
     * 测试多线程
     */
    private static void testMultiThread() {
        System.out.println("4.测试多线程");
        //创建5个线程，同时执行缓存操作
        for (int i = 1; i < 5; i++) {
            final int index = i;
            new Thread(() -> {
                CacheManager cache = CacheManager.INSTANCE;
                String key = "key-"+index;
                String value = "value-from-thread-"+index;
                cache.put(key, value);
                cache.get(key);
                //所有线程访问共享数据
                cache.put("shared-key", "shared-value");
                cache.get("shared-key");
            }, "Thread-"+index).start();
        }
        //等待线程执行完成
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace(System.err);
        }
        System.out.println("当前缓存大小："+CacheManager.INSTANCE.size());
    }
    /**
     * 测试类型安全获取
     */
    private static void testTypeSafeGet() {
        System.out.println("5.测试类型安全获取");
        CacheManager cache = CacheManager.INSTANCE;
        cache.put("user", new User("张三", 18));
        cache.put("stringValue", "hello world");
        cache.put("intValue", 123);
        //类型安全获取
        String str = cache.get("stringValue", String.class);
        Integer num = cache.get("intValue", Integer.class);
        User user = cache.get("user", User.class);
        String wrongType = cache.get("intValue", String.class); // 类型不匹配
        System.out.println("字符串值: " + str);
        System.out.println("整数值: " + num);
        System.out.println("用户对象: " + user);
        System.out.println("类型不匹配: " + wrongType);
        // 清理测试数据
        cache.clear();
    }


    static class User{
        private final String name;
        private final int age;
        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}
